import { Component, OnInit } from '@angular/core';
import {DataService} from '../data.service';
import {NzNotificationService} from 'ng-zorro-antd';


@Component({
  selector: 'app-oneroom',
  templateUrl: './oneroom.component.html',
  styleUrls: ['./oneroom.component.scss']
})
export class OneroomComponent implements OnInit {

  series = ['经济版', '舒适版', '尊享版'];

  isVisible = false;
  i = 2;
  editCache = {};  // 三方设备编辑缓存
  assist_editCache = {};  // 辅材编辑缓存
  demoValue = 10; // 折扣

  list: any[] = [];


  device_list = [
    {
      key    : '0',
      name : '总线电源',
      title : '总线电源',
      product_model : 'SBP2002.6440',
      brand : 'SATION',
      unit : '台',
      unit_price: 10 ,
      num: 1 ,
      total: 0,
      direction: 'right'
    },
    {
      key    : '1',
      name : 'APP网关',
      title : 'APP网关',
      product_model : 'LAN2300.0201',
      brand : 'SATION',
      unit : '台',
      unit_price: 20 ,
      num: 1 ,
      total: 0,
      direction: 'right'
    },
    {
      key    : '2',
      name : '6路0-10V调光执行器',
      title : '6路0-10V调光执行器',
      product_model : 'SDM0106.2000',
      brand : 'SATION',
      unit : '台',
      unit_price: 50 ,
      num: 1 ,
      total: 0,
      direction: 'left'
    },
    {
      key    : '3',
      name : '4路16A开关执行器',
      title : '4路16A开关执行器',
      product_model : 'SDO0004.1611',
      brand : 'SATION',
      unit : '台',
      unit_price: 100 ,
      num: 1 ,
      total: 0,
      direction: 'left'
    },
    {
      key    : '4',
      name : '红外学习器',
      title : '红外学习器',
      product_model : 'SDT3005.0101',
      brand : 'SATION',
      unit : '台',
      unit_price: 200 ,
      num: 1 ,
      total: 0,
      direction: 'right'
    },
    {
      key    : '5',
      name : '8键智能面板',
      title : '8键智能面板',
      product_model : 'MPA1308.8612',
      brand : 'SATION',
      unit : '台',
      unit_price: 1000 ,
      num: 1 ,
      total: 0,
      direction: 'right'
    },
    {
      key    : '6',
      name : '红外传感器',
      title : '红外传感器',
      product_model : 'SBP2002.6440',
      brand : 'SATION',
      unit : '台',
      unit_price: 25 ,
      num: 1 ,
      total: 0,
      direction: 'left'
    },
    {
      key    : '7',
      name : '2路窗帘执行器',
      title : '2路窗帘执行器',
      product_model : 'LAN2300.0201',
      brand : 'SATION',
      unit : '台',
      unit_price: 66 ,
      num: 1 ,
      total: 0,
      direction: 'left'
    },
    {
      key    : '8',
      name : '3路600W调光执行器',
      title : '3路600W调光执行器',
      product_model : 'SDM0106.2000',
      brand : 'SATION',
      unit : '台',
      unit_price: 150 ,
      num: 1 ,
      total: 0,
      direction: 'right'
    },
    {
      key    : '9',
      name : '12路16A开关执行器',
      title : '12路16A开关执行器',
      product_model : 'SDO0004.1611',
      brand : 'SATION',
      unit : '台',
      unit_price: 90 ,
      num: 1 ,
      total: 0,
      direction: 'right'
    },
    {
      key    : '10',
      name : '风机盘管执行器',
      title : '设备10',
      product_model : 'SDT3005.0101',
      brand : 'SATION',
      unit : '台',
      unit_price: 200 ,
      num: 1 ,
      total: 0,
      direction: 'left'
    },
    {
      key    : '11',
      name : 'DALI网关',
      title : 'DALI网关',
      product_model : 'MPA1308.8612',
      brand : 'SATION',
      unit : '台',
      unit_price: 1000 ,
      num: 1 ,
      total: 0,
      direction: 'left'
    }
  ];

  sation_device = [];

  other_device = [
    {
      key    : '0',
      name : '背景音乐-4分区',
      product_model : 'A75-4',
      brand : '泊声',
      unit : '套',
      unit_price: 45 ,
      num: 1 ,
      total: 0,
    },
    {
      key    : '1',
      name : '窗帘电机+4米导轨',
      product_model : 'DT300',
      brand : '杜亚',
      unit : '台',
      unit_price: 78 ,
      num: 1 ,
      total: 0,
    },
    {
      key    : '2',
      name : '魔镜',
      product_model : 'M1',
      brand : '锐吉',
      unit : '套',
      unit_price: 522 ,
      num: 1 ,
      total: 0,
    }
  ];

  assisted = [
    {
    key    : '0',
    name : 'KNX总线',
    product_model : 'J-Y(ST)YH-2*2*0.8',
    brand : 'VOKA',
    unit : '，米',
    unit_price: 2.5 ,
    num: 500 ,
    total: 0
    }
  ];

  sation_device_total = 0;
  other_device_total = 0;
  assisted_total = 0;
  total_price = 0;
  price_after_off = 0;


  constructor(private  data: DataService, private notification: NzNotificationService) { }


  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~模态框~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  showModal(): void {
    this.isVisible = true;
  }

  handleOk(): void {
    // console.log('Button ok clicked!');
    this.change_saiton_device();
    // console.log(this.sation_device);
    this.isVisible = false;
  }

  handleCancel(): void {
    // console.log('Button cancel clicked!');
    this.isVisible = false;
  }

  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~穿梭框~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  select(ret: {}): void {
    // console.log('nzSelectChange', ret);
    // console.log(ret['list']);
  }

  change(ret: {}): void {
    // console.log(ret);
    for (let arr of ret['list']){
      // console.log( this.device_list[arr.key]);
      // console.log(ret['to']);
      this.device_list[arr.key].direction = ret['to'];
    }

  }


  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~数字输入框~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  formatterPercent = value => `${value} %`;
  parserPercent = value => value.replace(' %', '');


  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~三方设备表格~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  addRow(): void {
    this.i++;
    this.other_device = [ ...this.other_device, {
      key  : `${this.i}`,
      name : '新设备',
      product_model : '型号',
      brand : '品牌',
      unit : '个',
      num: 1 ,
      unit_price: 0 ,
      total: 0,
    } ];
    this.updateEditCache();
  }

  deleteRow(i: string): void {
    const delete_device = this.other_device.filter(d => d.key !== i);
    this.other_device = delete_device;
  }

  startEdit(key: string, edit_num: number): void {
    this.editCache[ key ].edit = edit_num;
  }

  finishEdit(key: string , key_name: string): void {
    this.editCache[ key ].edit = false;

    switch (key_name) {
      case 'name': this.other_device.find(item => item.key === key).name = this.editCache[ key ].name; break;
      case 'product_model': this.other_device.find(item => item.key === key).product_model = this.editCache[ key ].product_model; break;
      case 'brand': this.other_device.find(item => item.key === key).brand = this.editCache[ key ].brand; break;
      case 'unit': this.other_device.find(item => item.key === key).unit = this.editCache[ key ].unit; break;
      case 'unit_price': this.other_device.find(item => item.key === key).unit_price = this.editCache[ key ].unit_price; break;
      case 'num': this.other_device.find(item => item.key === key).num = this.editCache[ key ].num; break;
      default: break;
    }

    this.calculate_total();
  }

  updateEditCache(): void {
    this.other_device.forEach(item => {
      if (!this.editCache[ item.key ]) {
        this.editCache[ item.key ] = {
          edit: 0,
          name: item.name,
          product_model :  item.product_model,
          brand : item.brand,
          unit : item.unit,
          unit_price: item.unit_price ,
          num: item.num ,
          total: item.total,

        };
      }
    });
    console.log(this.editCache);
  }

  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~辅材表格~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  add_assist() {

  }


  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~自定义~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  calculate_total() {
    this.price_after_off = 0;
    this.total_price = 0;
    this.sation_device_total = 0;
    this.other_device_total = 0;
    this.assisted_total = 0;
    this.price_after_off = 0;

    for (let arr of this.sation_device) {
      arr.total = arr.unit_price * arr.num;
      this.sation_device_total = this.sation_device_total + arr.total;
    }
    for (let arr of this.other_device) {
      arr.total = arr.unit_price * arr.num;
      this.other_device_total = this.other_device_total + arr.total;
    }
    for (let arr of this.assisted) {
      arr.total = arr.unit_price * arr.num;
      this.assisted_total = this.assisted_total + arr.total;
    }
    this.total_price = this.sation_device_total + this.other_device_total + this.assisted_total;
    this.price_after_off = this.total_price * ( 1 - this.demoValue / 100 );

  }

  change_saiton_device() {
    this.sation_device = [];
    for (let arr of this.device_list){
      if (arr.direction === 'right') {
        this.sation_device.push(arr);
      }
    }
  }

  ngOnInit(): void {
    this.updateEditCache();
    this.change_saiton_device();
    this.demoValue = 10;
    this.calculate_total();
  }


  export_excel() {
    // let filepath = '';
    // console.log(this.sation_device);
    this.data.sation_device = this.sation_device;
    this.data.other_device = this.other_device;
    this.data.assisted = this.assisted;
    this.data.export_excel();
    setTimeout(() => {
      // filepath = this.data.file;
      this.notification.blank( '导出成功', '导出地址: ./export2 /');
    }, 500);

  }

}
